﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class DayToDayManagement_ProductsStatistics : System.Web.UI.Page
{
    private bool IsUseColorSize
    {
        get
        {
            return new BLL.SYSPARAM().GetProductUpDownColorSizeConfig();
        }
    }

    private DataSet Data
    {
        get
        {
            DataSet ds = null;
            if (ViewState["Data"] != null)
            {
                ds=(DataSet)ViewState["Data"];
            }
            return ds;
        }
        set
        {
            ViewState["Data"] = value;
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        Check.CheckPermission("300100108");
        if (!IsPostBack)
        {
            string kwdm = ConfigurationManager.AppSettings["kwdm"];
            string cpkwdm = ConfigurationManager.AppSettings["cpkwdm"];
            ListItem[] items ={new ListItem("","-1"),
                              new ListItem(kwdm,kwdm),
                              new ListItem(cpkwdm,cpkwdm)
            };
            drpKW.Items.AddRange(items);

            AspNetPager1.PageSize = 50;
            AspNetPager2.PageSize = 50;
            BindDataListProducts();
        }
    }

    private void BindDataListProducts()
    {
        if (IsUseColorSize)
        {
            string StrSql = "";
            if (TextBox1.Text.Trim() != "")
            {
                if (DropDownList1.SelectedValue == "0")
                {
                    StrSql += " and CA_CODE like '%" + TextBox1.Text.Trim() + "%' ";
                }
                else if (DropDownList1.SelectedValue == "1")
                {
                    StrSql += " and SPMC like '%" + TextBox1.Text.Trim() + "%' ";
                }
                else if (DropDownList1.SelectedValue == "2")
                {
                    StrSql += " and CA_NAME like '%" + TextBox1.Text.Trim() + "%' ";
                }
            }
            //if (dropUp.SelectedValue == "1")
            //{
            //    StrSql += " and IsUp=1 ";
            //}
            //else if (dropUp.SelectedValue == "0")
            //{
            //    StrSql += " and IsUp=0 or isUp is null ";
            //}

            //if (dropPreUp.SelectedValue == "1")
            //{
            //    StrSql += " and IsPreUp=1 ";
            //}
            //else if (dropPreUp.SelectedValue == "0")
            //{
            //    StrSql += " and IsPreUp=0 or isPreUp is null ";
            //}
            if (dropUp.SelectedValue != "-1")
            {
                if (dropUp.SelectedValue == "1" && dropPreUp.SelectedValue == "0")
                {
                    StrSql += " and IsUp=1 and (IsPreUp=0 or isPreUp is null) ";
                }
                else if (dropUp.SelectedValue == "0" && dropPreUp.SelectedValue == "1")
                {
                    StrSql += " and (IsUp=0 or IsUp is null) and isPreUp=1 ";
                }
                else if (dropUp.SelectedValue == "1" && dropPreUp.SelectedValue == "1")
                {
                    StrSql += " and IsUp=1  and IsPreUp=1 ";
                }
                else if (dropUp.SelectedValue == "0" && dropPreUp.SelectedValue == "0")
                {
                    StrSql += " and (IsUp=0 or IsUp is null) and (isPreUp=0 or IsPreUp is null)";
                }

                else if (dropUp.SelectedValue == "1" && dropPreUp.SelectedValue == "-1")
                {
                    StrSql += " and IsUp=1 ";
                }
                else if (dropUp.SelectedValue == "0" && dropPreUp.SelectedValue == "-1")
                {
                    StrSql += " and (IsUp=0 or isUp is null) ";
                }

            }
            else if (dropPreUp.SelectedValue != "-1")
            {
                if (dropPreUp.SelectedValue == "1")
                {
                    StrSql += " and IsPreUp=1 ";
                }
                else if (dropPreUp.SelectedValue == "0")
                {
                    StrSql += " and (IsPreUp=0 or isPreUp is null) ";
                }
            }

            if(drpKW.SelectedValue!="-1")
            {
                StrSql += string.Format(" and CKDM='{0}' and KWDM='{1}' ", ConfigurationManager.AppSettings["ckdm"],drpKW.SelectedValue);
            }
            if (drpQTY.SelectedValue != "-1")
            {
                StrSql += string.Format(" |QTY{0}", drpQTY.SelectedItem.Text);
            }
            //StrSql += dropUp.SelectedIndex == 0 ? "" : " and IsUp=" + dropUp.SelectedValue + " or isUp is null ";
            //StrSql += dropPreUp.SelectedIndex == 0 ? "" : " and IsPreUp=" + dropPreUp.SelectedValue + " or isPreUp is null ";
            int pageCount, rowCount;
            DataSet ds = new BLL.ProductUpDown().GetProductsStatistics(GetStartIndex(AspNetPager1.CurrentPageIndex, AspNetPager1.PageSize), AspNetPager1.PageSize, StrSql, out rowCount);
            Data = ds;//导出数据
            dlistProducts.DataSource = ds.Tables[0];
            dlistProducts.DataBind();
            AspNetPager1.RecordCount = rowCount;
        }
        else
        {
            string StrSql = "";
            if (TextBox1.Text.Trim() != "")
            {
                if (DropDownList1.SelectedValue == "0")
                {
                    StrSql += " and CA_CODE like '%" + TextBox1.Text.Trim() + "%' ";
                }
                else if (DropDownList1.SelectedValue == "1")
                {
                    StrSql += " and SPMC like '%" + TextBox1.Text.Trim() + "%' ";
                }
                else if (DropDownList1.SelectedValue == "2")
                {
                    StrSql += " and CA_NAME like '%" + TextBox1.Text.Trim() + "%' ";
                }
            }
            //StrSql += dropUp.SelectedIndex == 0 ? "" : " and IsUp=" + dropUp.SelectedValue + " or isUp is null ";
            // StrSql += dropPreUp.SelectedIndex == 0 ? "" : " and IsPreUp=" + dropPreUp.SelectedValue + " or isPreUp is null ";
            if (dropUp.SelectedValue != "-1")
            {
                if (dropUp.SelectedValue == "1" && dropPreUp.SelectedValue == "0")
                {
                    StrSql += " and IsUp=1  and (IsPreUp=0 or isPreUp is null) ";
                }
                else if (dropUp.SelectedValue == "0" && dropPreUp.SelectedValue == "1")
                {
                    StrSql += " and (IsUp=0 or IsUp is null) and isPreUp=1 ";
                }
                else if (dropUp.SelectedValue == "1" && dropPreUp.SelectedValue == "1")
                {
                    StrSql += " and IsUp=1  and IsPreUp=1 ";
                }
                else if (dropUp.SelectedValue == "0" && dropPreUp.SelectedValue == "0")
                {
                    StrSql += " and (IsUp=0 or IsUp is null) and (isPreUp=0 or isPreUp is null) ";
                }

                else if (dropUp.SelectedValue == "1" && dropPreUp.SelectedValue == "-1")
                {
                    StrSql += " and IsUp=1 ";
                }
                else if (dropUp.SelectedValue == "0" && dropPreUp.SelectedValue == "-1")
                {
                    StrSql += " and (IsUp=0 or isUp is null) ";
                }

            }
            else if (dropPreUp.SelectedValue != "-1")
            {
                if (dropPreUp.SelectedValue == "1")
                {
                    StrSql += " and IsPreUp=1 ";
                }
                else if (dropPreUp.SelectedValue == "0")
                {
                    StrSql += " and (IsPreUp=0 or isPreUp is null) ";
                }
            }
            if (drpKW.SelectedValue != "-1")
            {
                StrSql += string.Format(" and CKDM='{0}' and KWDM='{1}' ", ConfigurationManager.AppSettings["ckdm"], drpKW.SelectedValue);
            }
            if (drpQTY.SelectedValue != "-1")
            {
                StrSql += string.Format(" |QTY{0}", drpQTY.SelectedItem.Text);
            }
            int pageCount, rowCount;
            DataSet ds = new BLL.ProductUpDown().GetProductsStatisticsWithoutColorSize(GetStartIndex(AspNetPager2.CurrentPageIndex, AspNetPager2.PageSize), AspNetPager2.PageSize, StrSql, out rowCount);
            Data = ds;//导出数据
            dlistProducts2.DataSource = ds.Tables[0];
            dlistProducts2.DataBind();

            AspNetPager2.RecordCount = rowCount;
        }
    }

    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        BindDataListProducts();
    }
    protected void AspNetPager2_PageChanged(object sender, EventArgs e)
    {
        BindDataListProducts();
    }

    private int GetStartIndex(int pageIndex, int pageSize)
    {
        return 1 + (pageIndex - 1) * pageSize;
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        AspNetPager1.CurrentPageIndex = 1;
        AspNetPager2.CurrentPageIndex = 1;
        BindDataListProducts();
    }
    protected void btnExport_Click(object sender, EventArgs e)
    {
        DataTable2Excel(Data.Tables[1]);
    }
    /// <summary>
    /// DataTable导出Excel
    /// </summary>
    /// <param name="dtData"></param>
    public static void DataTable2Excel(System.Data.DataTable dtData)
    {
        System.Web.UI.WebControls.DataGrid dgExport = null;
        System.Web.HttpContext curContext = System.Web.HttpContext.Current;
        System.IO.StringWriter strWriter = null;
        System.Web.UI.HtmlTextWriter htmlWriter = null;

        if (dtData != null)
        {
            curContext.Response.ContentType = "application/vnd.ms-excel";
            curContext.Response.ContentEncoding = System.Text.Encoding.UTF8;
            curContext.Response.Charset = "";

            strWriter = new System.IO.StringWriter();
            htmlWriter = new System.Web.UI.HtmlTextWriter(strWriter);

            dgExport = new System.Web.UI.WebControls.DataGrid();

            dtData.Columns.Add("预上架");
            dtData.Columns.Add("上架");

            dtData.Columns["CA_CODE"].ColumnName = "商品代码";
            dtData.Columns["SPMC"].ColumnName = "ERP名称";
            dtData.Columns["CA_NAME"].ColumnName = "电子商务名称";

            dtData.Columns["COLORNAME"].ColumnName = "颜色";
            dtData.Columns["SIZENAME"].ColumnName = "尺码";
            dtData.Columns["QTY"].ColumnName = "库存";
            dtData.Columns["ACTUALQTY"].ColumnName = "实际库存";

            for (int i = 0; i < dtData.Rows.Count; i++)
            {
                dtData.Rows[i]["预上架"] = dtData.Rows[i]["ISPREUP"].ToString() == "1" ? "是" : "否";
                dtData.Rows[i]["上架"] = dtData.Rows[i]["ISUP"].ToString() == "1" ? "是" : "否";
            }
            dtData.Columns.Remove(dtData.Columns["ID"]);
            dgExport.DataSource = dtData.DefaultView;
            dgExport.AllowPaging = false;
            dgExport.DataBind();

            dgExport.RenderControl(htmlWriter);
            curContext.Response.Write(strWriter.ToString());
            curContext.Response.End();
        }
    }
}
